/**
 * 注册
 * @author Qasim <15750783791@163.com>
 * @version 2016/10/29
 */
var self = id = '';
document.addEventListener('plusready' , ready ,  false);

function ready(){
	
	self = plus.webview.currentWebview();
	
	var phone = '';
	
	id = self.aid;
	
	id ? $('.title').text('完善资料') : $('.title').text('注册');
	
	//获取验证码
	$('.verification').on('click' , function(){
		phone = $('input[name=phone]').val();
		if(!p1.test(phone)){
			toast('手机号码格式有误'); return;
		}
		id ? getverify(phone) : checkUser(phone); 
	})
	
	$('.register').on('click' , function(){
		var pass = $('input[name=pass]').val() , 
			verify = $('input[name=verification]').val(), 
			repass = $('input[name=repass]').val(), 
			email = $('input[name=email]').val();
		if(!phone)return;	 
		if(!p3.test(email)){
			toast('邮箱格式有误'); return;
		}
		if(!pass.trim()){
			toast('密码不能为空'); return;
		}
		if(pass !== repass){
			toast('两次密码不一致'); return;
		}
		var verify_local = plus.storage.getItem('verify');
		if(!verify.trim() || !verify_local || verify_local != verify){
			toast('验证码输入有误');return;
		}
		plus.nativeUI.showWaiting('注册中...');
		$.ajax({
			url : apiRoot + '/Home/Member/register/' , 
			type : 'post' , 
			data : {
				aid : id,
				phone : phone , 
				pass : pass ,
				email : email
			} , 
			success : function(data){
				plus.nativeUI.closeWaiting();
				plus.storage.removeItem('verify');
				if(data){
					if(id){
						toast('登录成功');
						//保存用户数据
						plus.storage.setItem('memberInfo' , data + '')
						openNewPage('book.html' , self);
					}else{
						toast('注册成功');
						openNewPage('login.html' , self);
					}
				}
			} , 
			error : function(e){
				errorPrompt(e);
			}
		})
	})
	
}
